//分页
function pagination(data) {

    $("#total").html("");
    $("#totalPages").html("");
    $("#now").html("");
    $("#pager").html("");

    var total = data.data.total;
    var pages = data.data.pages;
    var now = data.data.pageNum;
    var beforePage = data.data.prePage;
    var nextPage = data.data.nextPage;

    var pagesHtml = "";
    var pageStart = "<a class=\"paginate_button previous disabled\" onclick='pageSkip(" + beforePage + ")' id=\"table_previous\">上一页</a>\n" +
        "            <span id=\"pages\">";
    var pageEnd = "</span>\n" +
        "            <a class=\"paginate_button next disabled\" onclick='pageSkip(" + nextPage + ")' id=\"table_next\">下一页</a>";

    if (pages <= 5) {
        for (var k = 0; k < pages; k++) {
            if ((k + 1) == now) {
                var pageHtml = "<a class=\"paginate_button current\" onclick='pageSkip(" + (k + 1) + ")'>" + (k + 1) + "</a>";
            } else {
                var pageHtml = "<a class=\"paginate_button \" onclick='pageSkip(" + (k + 1) + ")'>" + (k + 1) + "</a>";
            }

            pagesHtml = pagesHtml + pageHtml;
        }

    } else {
        if (now > 4 && now < (pages - 3)) {
            var pagesHtml = "<a class=\"paginate_button\" onclick='pageSkip(1)'>1</a>" +
                "<span> …</span>";
            for (i = (now - 1); i < now + 2; i++) {
                if (i == now) {
                    var pageHtml = "<a class=\"paginate_button current\" onclick='pageSkip(" + (i) + ")'>" + (i) + "</a>";
                } else {
                    var pageHtml = "<a class=\"paginate_button \" onclick='pageSkip(" + (i) + ")'>" + (i) + "</a>";
                }
                pagesHtml = pagesHtml + pageHtml;
            }
            pagesHtml = pagesHtml + "<span> …</span>\n" +
                "<a class=\"paginate_button \" onclick='pageSkip(" + pages + ")'>" + pages + "</a>";


        } else if (now > (pages - 4) && now <= pages) {
            var pagesHtml = "<a class=\"paginate_button\" onclick='pageSkip(1)'>1</a>" +
                "<span> …</span>";
            for (i = (pages - 4); i <= pages; i++) {
                if (i == now) {
                    var pageHtml = "<a class=\"paginate_button current\" onclick='pageSkip(" + (i) + ")'>" + (i) + "</a>";
                } else {
                    var pageHtml = "<a class=\"paginate_button \" onclick='pageSkip(" + (i) + ")'>" + (i) + "</a>";
                }
                pagesHtml = pagesHtml + pageHtml;

            }

        } else if (now > 0 && now <= 4) {

            for (var i = 0; i <= 4; i++) {
                if ((i + 1) == now) {
                    var pageHtml = "<a class=\"paginate_button current\" onclick='pageSkip(" + (i + 1) + ")'>" + (i + 1) + "</a>";
                } else {
                    var pageHtml = "<a class=\"paginate_button \" onclick='pageSkip(" + (i + 1) + ")'>" + (i + 1) + "</a>";
                }

                pagesHtml = pagesHtml + pageHtml;
            }
            pagesHtml = pagesHtml + "<span> …</span>\n" +
                "<a class=\"paginate_button \" onclick='pageSkip(" + pages + ")'>" + pages + "</a>";
        }

    }

    $("#total").append(total);
    $("#totalPages").append(pages);
    $("#now").append(now);
    $("#pager").append(pageStart);
    $("#pages").append(pagesHtml);
    $("#pager").append(pageEnd);

}

//获取用户组
function userGroup(){
    var usergroup=null;
    $.ajaxSetup({async : false});
    $.ajax({
        type: "get",
        url: "/enum/groupEnum",
        data: {
        },
        dataType: "json",
        success: function (data) {
            usergroup=data;
        },
        fail:function (data) {

            alert(JSON.stringify(data));
        },
        error: function(xhr) {
            alert('Error' + JSON.stringify(xhr));
        }
    });
    return usergroup;
}

//获取上个页面传过来的参数
function getParameter(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//删除列表数据
function del(obj, id, url, ms) {
    /* if (ms == null || ms == "") {
         ms = "请确认是否要删除id=" + id + "的数据！";
     }*/
    if(confirm(ms) == true) {
        $.ajax({
            type: 'POST',
            url: url,
            data: {
                id: id
            },
            dataType: 'json',
            success: function (data) {
                var status = data.status;
                var msg = data.msg;
                if (status == 0) {
                    $(obj).parents("tr").remove();
                    pageSkip(1);
                    alert('删除成功!');
                } else {
                    alert(msg);
                }

            },
            error: function (data) {
                console.log(data);
            },
        });
    };
}
